/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.functionality;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import softstarter.xmlhandling.XMLDocumentReader;
import softstarter.xmlhandling.XMLDocumentWriter;

public class FileCentre {

	private static final String DOCUMENT_NAME = "resources/fileobjects.xml";
	private XMLDocumentReader xmlReader;
	private XMLDocumentWriter xmlWriter;
	private List<RunnableItem> runnableItems;
	
	public FileCentre() {
		xmlReader = new XMLDocumentReader();
		xmlWriter = new XMLDocumentWriter();
		runnableItems = new LinkedList<RunnableItem>();
		readFilesFromDisk();
	}
	
	private void readFilesFromDisk() {
		runnableItems = xmlReader.parseFileObjects(DOCUMENT_NAME);
	}

	public List<RunnableItem> getFiles() {
		return runnableItems;
	}
	
	public void addJavaFiles(List<File> files) {
		for(File file : files)
			addFile(new RunnableItem(file.getName(), file.getAbsolutePath()));
	}
	
	public void addFiles(List<RunnableItem> files) {
		for(RunnableItem file : files)
			addFile(file);
	}
	
	public void addFile(RunnableItem file) {
		if(!runnableItems.contains(file))
			runnableItems.add(file);
		saveFilesToDisk();
	}
	
	public void removeFile(RunnableItem file) {
		runnableItems.remove(file);
		saveFilesToDisk();
	}

	private boolean saveFilesToDisk() {
		return(xmlWriter.writeFileObjectsToXML(DOCUMENT_NAME, runnableItems));
	}
	
	public static void main(String[] args) {
		System.out.println(System.getProperty("user.dir"));
	}

}
